package com.wyp168.gulimall.product.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wyp168.gulimall.product.dao.AttrAttrgroupRelationDao;
import com.wyp168.gulimall.product.entity.AttrAttrgroupRelationEntity;
import com.wyp168.gulimall.product.vo.AttrGroupRelationVO;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wyp168.common.utils.PageUtils;
import com.wyp168.common.utils.Query;

import com.wyp168.gulimall.product.dao.AttrGroupDao;
import com.wyp168.gulimall.product.entity.AttrGroupEntity;
import com.wyp168.gulimall.product.service.AttrGroupService;


@Service("attrGroupService")
public class AttrGroupServiceImpl extends ServiceImpl<AttrGroupDao, AttrGroupEntity> implements AttrGroupService {

    @Autowired
    private AttrGroupDao attrGroupDao;

    @Autowired
    private AttrAttrgroupRelationDao relationDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrGroupEntity> page = this.page(
                new Query<AttrGroupEntity>().getPage(params),
                new QueryWrapper<AttrGroupEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public PageInfo<AttrGroupEntity> queryPage(Map<String, String> params, Long cateLogId) {
        PageInfo<AttrGroupEntity> pageInfo;
        PageHelper.startPage(Integer.parseInt(params.get("page")), Integer.parseInt(params.get("limit")));
        String key = String.valueOf(params.get("key"));
        if (Objects.isNull(cateLogId) || cateLogId == 0L) {
            QueryWrapper<AttrGroupEntity> queryWrapper = new QueryWrapper<>();
            if (!StringUtils.isEmpty(key)) {
                queryWrapper.eq("attr_group_id", key)
                        .or()
                        .like("attr_group_name", key);
            }
            List<AttrGroupEntity> attrGroupEntities = baseMapper.selectList(queryWrapper);
            pageInfo = new PageInfo<>(attrGroupEntities);
        } else {
            //select * from pms_attr_catelog where catelog_id = cateLogId and (attr_group_id = key or attr_groupname like %key%);
            //检查分页参数合法性
            List<AttrGroupEntity> attrGroupEntities = baseMapper.selectByIdAndName(cateLogId, key);
            pageInfo = new PageInfo<>(attrGroupEntities);
        }
        return pageInfo;
    }

    @Override
    public void deleteRelation(List<AttrGroupRelationVO> vos) {
        List<AttrAttrgroupRelationEntity> entities = vos.stream().map(item -> {
            AttrAttrgroupRelationEntity attrGroupRelationEntity = new AttrAttrgroupRelationEntity();
            BeanUtils.copyProperties(item, attrGroupRelationEntity);
            return attrGroupRelationEntity;
        }).collect(Collectors.toList());
        relationDao.deleteBatchRelation(entities);
    }

}